﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:FinanceProp
    /// </summary>
    public partial class FinanceProp
    {
        public FinanceProp()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FFinancePropNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fund.t_FinanceProp");
            strSql.Append(" where FFinancePropNo=@FFinancePropNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6)		};
            parameters[0].Value = FFinancePropNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public string GetFFinancePropNoByFFinancePropShortName(string FFinancePropShortName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 FFinancePropNo FROM fund.t_FinanceProp WHERE FFinancePropShortName=@FFinancePropShortName");
            SqlParameter[] parameters = {
					new SqlParameter("@FFinancePropShortName", SqlDbType.NVarChar,20)		};
            parameters[0].Value = FFinancePropShortName;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }
        /// <summary>
        /// 获取下拉列表数据
        /// </summary>
        /// <param name="fYear"></param>
        /// <param name="FEnterprisePropNo"></param>
        /// <returns></returns>
        public DataSet GetSelectList( string FEnterprisePropNo,string fyear)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT b.FFinancePropNo,b.FFinancePropName,b.FFinancePropShortName FROM fund.t_EnterpriseFinanceProp a LEFT JOIN fund.t_FinanceProp b ON a.FFinancePropNo = b.FFinancePropNo 
                            WHERE FEnterprisePropNo='" + FEnterprisePropNo + "' and FYear=" + fyear + "");
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(EnterpriseFund.Model.FinanceProp model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into fund.t_FinanceProp(");
            strSql.Append("FFinancePropNo,FFinancePropName,FFinancePropShortName)");
            strSql.Append(" values (");
            strSql.Append("@FFinancePropNo,@FFinancePropName,@FFinancePropShortName)");
            SqlParameter[] parameters = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FFinancePropName", SqlDbType.NVarChar,100),
					new SqlParameter("@FFinancePropShortName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.FFinancePropNo;
            parameters[1].Value = model.FFinancePropName;
            parameters[2].Value = model.FFinancePropShortName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(EnterpriseFund.Model.FinanceProp model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update fund.t_FinanceProp set ");
            strSql.Append("FFinancePropName=@FFinancePropName,");
            strSql.Append("FFinancePropShortName=@FFinancePropShortName");
            strSql.Append(" where FFinancePropNo=@FFinancePropNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFinancePropName", SqlDbType.NVarChar,100),
					new SqlParameter("@FFinancePropShortName", SqlDbType.NVarChar,50),
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6)};
            parameters[0].Value = model.FFinancePropName;
            parameters[1].Value = model.FFinancePropShortName;
            parameters[2].Value = model.FFinancePropNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FFinancePropNo)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            strSql.Append("delete from fund.t_EnterpriseFinanceProp ");
            strSql.Append(" where FFinancePropNo=@FFinancePropNo  ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6)		};
            parameters[0].Value = FFinancePropNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("delete from fund.t_FinanceProp ");
            strSql.Append(" where FFinancePropNo=@FFinancePropNo ");
            SqlParameter[] parameters2 = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6)		};
            parameters2[0].Value = FFinancePropNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }

        }

       
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.FinanceProp GetModel(string FFinancePropNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FFinancePropNo,FFinancePropName,FFinancePropShortName from fund.t_FinanceProp ");
            strSql.Append(" where FFinancePropNo=@FFinancePropNo  ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6)		};
            parameters[0].Value = FFinancePropNo;

            EnterpriseFund.Model.FinanceProp model = new EnterpriseFund.Model.FinanceProp();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.FinanceProp DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.FinanceProp model = new EnterpriseFund.Model.FinanceProp();
            if (row != null)
            {
                if (row["FFinancePropNo"] != null)
                {
                    model.FFinancePropNo = row["FFinancePropNo"].ToString();
                }
                if (row["FFinancePropName"] != null)
                {
                    model.FFinancePropName = row["FFinancePropName"].ToString();
                }
                if (row["FFinancePropShortName"] != null)
                {
                    model.FFinancePropShortName = row["FFinancePropShortName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FFinancePropNo,FFinancePropName,FFinancePropShortName ");
            strSql.Append(" FROM fund.t_FinanceProp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FFinancePropNo,FFinancePropName,FFinancePropShortName ");
            strSql.Append(" FROM fund.t_FinanceProp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fund.t_FinanceProp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FYear desc");
            }
            strSql.Append(")AS Row, T.*  from fund.t_FinanceProp T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_FinanceProp";
            parameters[1].Value = "FYear";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
